package com.dlc.commonlibrary.widget.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.dlc.commonlibrary.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CommonPopupList extends PopupWindow {

    private final Context mContext;

    private final ListView mListView;
    private final InnerAdapter mAdapter;
    private int mDefaultMargin;

    private boolean mDimmerBackground;
    private OnSelectItemListener mOnSelectItemListener;

    public interface OnSelectItemListener {
        void onSelectItem(CommonPopupList popup, String item, int position);
    }

    public interface ItemConverter<T> {
        String covert(T t);
    }

    public CommonPopupList(Context context) {
        super(context);
        mContext = context;

        View view = LayoutInflater.from(context).inflate(R.layout.pop_common_list, null);

        mListView = (ListView) view.findViewById(R.id.list_view);
        mAdapter = new InnerAdapter();
        mListView.setAdapter(mAdapter);

        mDefaultMargin = getDimen(R.dimen.normal_1dp);

        setContentView(view);
        setAnimationStyle(R.style.PopupStyle);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setWidth(getDimen(R.dimen.normal_300dp));

        update();
    }

    public static Builder newBuilder(Context context) {
        return new Builder(context);
    }

    public static class Builder {

        private final CommonPopupList mPopupList;

        public Builder(Context context) {

            mPopupList = new CommonPopupList(context);
        }

        public Builder setOnSelectItemListener(final OnSelectItemListener onSelectItemListener) {
            mPopupList.setOnSelectItemListener(onSelectItemListener);
            return this;
        }

        public Builder setWidthEquals(final View view) {
            mPopupList.setWidthEquals(view);
            return this;
        }

        public Builder setDimmerBackground(boolean dimmerBackground) {
            mPopupList.setDimmerBackground(dimmerBackground);
            return this;
        }

        public Builder setData(List<String> items) {
            mPopupList.setData(items);
            return this;
        }

        public Builder setData(String[] items) {
            mPopupList.setData(items);
            return this;
        }

        public <T> Builder setData(List<T> items, @NonNull ItemConverter<T> converter) {
            mPopupList.setData(items, converter);
            return this;
        }

        public <T> Builder setData(T[] items, @NonNull ItemConverter<T> converter) {
            mPopupList.setData(items, converter);
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            mPopupList.setOnDismissListener(onDismissListener);
            return this;
        }

        public CommonPopupList build() {
            return mPopupList;
        }
    }

    /**
     * 获取尺寸简便方法
     *
     * @param resId
     * @return
     */
    private int getDimen(int resId) {
        return mContext.getResources().getDimensionPixelSize(resId);
    }

    private void setOnSelectItemListener(final OnSelectItemListener onSelectItemListener) {

        if (onSelectItemListener != null) {
            mOnSelectItemListener = onSelectItemListener;
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectItem(position);
                }
            });
        }
    }

    /**
     * 选择某项
     *
     * @param position
     */
    public void selectItem(int position) {
        if (mOnSelectItemListener != null) {
            String item = mAdapter.getItem(position);
            mOnSelectItemListener.onSelectItem(this, item, position);
        }
    }

    private void setWidthEquals(final View view) {
        view.post(new Runnable() {
            @Override
            public void run() {
                setWidth(view.getWidth());
                update();
            }
        });
    }

    private void setDimmerBackground(boolean dimmerBackground) {
        mDimmerBackground = dimmerBackground;
    }

    public void setData(List<String> items) {
        mAdapter.setNewData(items);
    }

    public void setData(List<String> items, boolean selectFirst) {
        setData(items);
        if (selectFirst && mAdapter.getCount() > 0) {
            selectItem(0);
        }
    }

    public void setData(String[] items) {
        if (items != null) {
            mAdapter.setNewData(Arrays.asList(items));
        } else {
            mAdapter.setNewData(null);
        }
    }

    public void setData(String[] items, boolean selectFirst) {
        setData(items);
        if (selectFirst && mAdapter.getCount() > 0) {
            selectItem(0);
        }
    }

    public <T> void setData(List<T> items, @NonNull ItemConverter<T> converter) {
        if (items != null && items.size() > 0) {
            ArrayList<String> strings = new ArrayList<>();
            for (T item : items) {
                strings.add(converter.covert(item));
            }
            mAdapter.setNewData(strings);
        } else {
            mAdapter.setNewData(null);
        }
    }

    public <T> void setData(List<T> items, @NonNull ItemConverter<T> converter,
        boolean selectFirst) {
        setData(items, converter);
        if (selectFirst && mAdapter.getCount() > 0) {
            selectItem(0);
        }
    }

    public <T> void setData(T[] items, @NonNull ItemConverter<T> converter) {
        if (items != null && items.length > 0) {
            ArrayList<String> strings = new ArrayList<>();
            for (T item : items) {
                strings.add(converter.covert(item));
            }
            mAdapter.setNewData(strings);
        } else {
            mAdapter.setNewData(null);
        }
    }

    public <T> void setData(T[] items, @NonNull ItemConverter<T> converter, boolean selectFirst) {
        setData(items, converter);
        if (selectFirst && mAdapter.getCount() > 0) {
            selectItem(0);
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor, 0, mDefaultMargin);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        if (mDimmerBackground) {
            setBackground(true);
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        if (mDimmerBackground) {
            setBackground(true);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mDimmerBackground) {
            setBackground(false);
        }
    }

    private void setBackground(boolean dimmer) {

        Window window = ((Activity) mContext).getWindow();

        WindowManager.LayoutParams mLayoutParams = window.getAttributes();
        mLayoutParams.alpha = dimmer ? 0.8f : 1f;
        window.setAttributes(mLayoutParams);
    }

    private static final class InnerAdapter extends BaseAdapter {

        private final ArrayList<String> mItems;

        public InnerAdapter() {

            mItems = new ArrayList<>();
        }

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public String getItem(int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void setNewData(List<String> strings) {
            mItems.clear();
            if (strings != null) {
                mItems.addAll(strings);
            }
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView) convertView;
            if (view == null) {
                view = (TextView) LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_common_popup, parent, false);
            }

            String item = getItem(position);
            view.setText(item);

            return view;
        }
    }
}
